<template>
  <div class="detail-enterprise">
    <header class="add-header">
      <el-page-header content="查看企业" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">租赁记录</div>
        <div class="table-container">
          <el-table
            :data="form.rent"
            style="width: 100%"
            border
          >
            <el-table-column
              type="index"
              label="序号"
              width="50"
            />
            <el-table-column
              prop="name"
              label="租赁楼宇"
              width="180"
            />
            <el-table-column
              label="租赁起止时间"
              width="280"
            >
              <template #default="{row}">
                {{ row.startTime }} - {{ row.endTime }}
              </template>
            </el-table-column>
            <el-table-column
              label="租赁合同(点击预览)"
            >
              <template #default="{row}">
                <el-button type="text">
                  <a :href="row.contractUrl" target="_blank">  {{ row.contractName }}</a>
                  <!-- <iframe :src="row.contractUrl" width="100%" height="600px" /> -->
                </el-button>
              </template>
            </el-table-column>
            <el-table-column
              prop="createTime"
              label="录入时间"
            />
            <el-table-column
              prop="address"
              label="操作"
            >
              <template #default="{row}">
                <el-button type="text" @click="uploadFileFn(row)">合同下载</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import { enterpriseDetailInfoApi, uploaddownloadApi } from '@/apis/Park'
export default {
  data() {
    return {
      form: {},
      previewURL: 'https://view.officeapps.live.com/op/view.aspx?src='
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    // 获取初始化数据
    async getDetail() {
      const res = await enterpriseDetailInfoApi(this.$route.query.id)
      res.data.rent.forEach(item => {
        const url = item.contractUrl
        const extName = url.slice(url.lastIndexOf('.'))
        if (extName === '.docx' || extName === '.doc') {
          item.contractUrl = this.previewURL + item.contractUrl
        }
      })
      this.form = res.data
    },
    // 合同下载
    async uploadFileFn(row) {
    //   console.log(row)
      const res = await uploaddownloadApi(row.contractId)
      //   console.log(res)

      // 创建 Blob 对象，并使用 URL.createObjectURL 来生成下载链接，最终通过创建 <a> 元素并触发点击实现文件下载。
      const Url = window.URL.createObjectURL(res)

      const a = document.createElement('a')

      a.href = Url
      a.download = row.contractName
      a.click()
    }
  }
}
</script>
